#include "chooselevelscene.h"

#include <QMenuBar>
#include <QMessageBox>
#include <QPainter>
#include <mypushbutton.h>
#include <QTimer>
#include <QLabel>
#include <QSound>
ChooseLevelScene::ChooseLevelScene(QWidget *parent) : QMainWindow(parent)
{
    //设置窗口固定大小
    this->setFixedSize(320,588);
    // 设置图标
    this->setWindowIcon(QIcon(":/img/res/Coin0001.png"));
    // 设置标题
    this->setWindowTitle("选择关卡场景");

    // 创建菜单栏
    QMenuBar * bar = menuBar();
    setMenuBar(bar);
    // 开始创建菜单
    QMenu * menu = bar->addMenu("开始");
    // 创建退出菜单项
    QAction * actionQuit = menu->addAction("退出");
    // 点击退出 结束
    connect(actionQuit,&QAction::triggered,this,[=](){
        QMessageBox::StandardButton flag = QMessageBox::question(this,"要退出？","确定退出吗？",QMessageBox::Yes | QMessageBox::No);
        if(flag)
            this->close();
    });
    // 返回音效
    QSound *backSound = new QSound(":/img/res/BackButtonSound.wav",this);

    // 返回按钮
    MyPushButton *btn_back = new MyPushButton(":/img/res/BackButton.png",":/img/res/BackButtonSelected.png");
    btn_back->setParent(this);
    btn_back->move(this->width()-btn_back->width(),this->height()-btn_back->height());
    connect(btn_back,&QPushButton::clicked,[=](){
        backSound->play();
        emit this->chooseSceneBack();
    });

    // 创建选择的关卡 选择音效
    QSound *chooseSound = new QSound(":/img/res/TapButtonSound.wav",this);
    for(int i=0;i<20;i++){
        MyPushButton * btn = new MyPushButton(":/img/res/LevelIcon.png");
        btn->setParent(this);
        btn->move(25 + (i%4)*70 , 130+ (i/4)*70);

        // 监听每个按钮的点击事件
        connect(btn,&QPushButton::clicked,this,[=](){
           // 选择关卡音效
           chooseSound->play();
           emit this->chooseLevel(i + 1);
        });

        // 对图片添加数组
        QLabel * label = new QLabel;
        label->setParent(this);
        label->setText(QString::number(i+1));
        label->setFixedSize(btn->size());
        // 设置文本居中
        label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        // 由于label会导致鼠标无法点击按钮 因此需要进行鼠标穿透
        label->setAttribute(Qt::WA_TransparentForMouseEvents,true);
        label->move(25 + (i%4)*70 , 130+ (i/4)*70);
    }
}

void ChooseLevelScene::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/img/res/OtherSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //加载标题
    pix.load(":/img/res/Title.png");
    painter.drawPixmap( (this->width() - pix.width())*0.5,30,pix.width(),pix.height(),pix);

}
